package com.flowerSystem.service.impl;

import com.flowerSystem.entity.Coupon;
import com.flowerSystem.mapper.CouponMapper;
import com.flowerSystem.service.CouponService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 鲜花遇上牛战士
 * @since 2023-08-28
 */
@Service
public class CouponServiceImpl extends ServiceImpl<CouponMapper, Coupon> implements CouponService {
    @Autowired
    CouponMapper couponMapper;
    @Override
    @Transactional
    public boolean receive(int id) {
        Coupon coupon = couponMapper.selectById(id);
        if (coupon.getNumber()==0){
            return false;
        }
        coupon.setNumber(coupon.getNumber()-1);
        try {
            couponMapper.updateById(coupon);
            return true;
        }catch (Exception e){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
    }

    @Override
    public List<Coupon> getCouponByuserId(int userId) {
        return couponMapper.getCouponByUserId(userId);
    }

    @Override
    public boolean upUserCoupon(int user_id, int coupon_id) {
        return couponMapper.upUserCoupon(user_id,coupon_id);
    }
}
